package part_2;

/*
   Purpose:
     This class demonstrates how to read data from a disk file,
     converting it to double or int values.  It traps exceptions
     and allows the user to recover from errors, when possible.

   Record of revisions:
       Date       Programmer          Description of change
       ====       ==========          =====================
    05/24/2002   S. J. Chapman        Original code
*/
import java.io.*;
import java.util.*;
public class ReadFile {

   // Declare instance variables
   BufferedReader br;              // Reference to BufferedReader
   StringTokenizer st;             // Reference to StringTokenizer

   // Constructor
   public ReadFile(String fileName) throws FileNotFoundException,
                                           IOException {

      // Local variables
      boolean inputOk = false;      // Input OK flag

      // Open buffered reader
      br = new BufferedReader(new FileReader(fileName));

      // Get the first line of valid data in a StringTokenizer
      while ( !inputOk ) {
         try {
            st = new StringTokenizer( br.readLine() );
            inputOk = true;
         }
         catch ( NoSuchElementException e ) {

            // Blank line, so skip it and try again
            inputOk = false;
         }
         catch ( IOException e ) {

            // We can't do anything about this, so re-throw it
            throw e;
         }
      }
   }


   // Read a double value from the file
   public double readDouble() throws IOException {

      // Local variables
      double d = 0;                 // Double value
      boolean inputOk = false;      // Input OK flag
      String str;                   // Input string

      // Get a double value
      while ( !inputOk ) {
         try {
            if (st.hasMoreTokens()) {

               // Get the next value from the current line
               d = Double.parseDouble(st.nextToken());
               inputOk = true;
            }
            else {

               // Get a new line and try again
               str = br.readLine();
               if ( str != null ) {
                  st = new StringTokenizer(str);
                  inputOk = false;
               }
               else {

                  // At EOF, so throw EOF exception
                  throw new EOFException();
               }
            }
         }
         catch ( NoSuchElementException e ) {

            // Blank line, so skip it and try again
            inputOk = false;
         }
      }
      return d;
   }


   // Read an int value from the file
   public int readInt() throws IOException {

      // Local variables
      int i = 0;                    // int value
      boolean inputOk = false;      // Input OK flag
      String str;                   // Input string

      // Get a double value
      while ( !inputOk ) {
         try {
            if (st.hasMoreTokens()) {

               // Get the next value from the current line
               i = Integer.parseInt(st.nextToken());
               inputOk = true;
            }
            else {

               // Get a new line and try again
               str = br.readLine();
               if ( str != null ) {
                  st = new StringTokenizer(str);
                  inputOk = false;
               }
               else {

                  // At EOF, so throw EOF exception
                  throw new EOFException();
               }
            }
         }
         catch ( NoSuchElementException e ) {

            // Blank line, so skip it and try again
            inputOk = false;
         }
      }
      return i;
   }


   // Method to close the file
   public void close() throws IOException {
      br.close();
   }
}
